package com.wy.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wy.common.result.Result;
import com.wy.model.entity.Role;
import com.wy.model.vo.AssignRoleVo;
import com.wy.model.vo.RoleQueryVo;
import com.wy.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("admin/system/role")
public class RoleController {
  @Autowired
  private RoleService roleService;

  //查询所有Role
  @GetMapping("findAll")
  public Result findAllRole(){
    List<Role> list = roleService.list();
    return Result.ok(list);
  }

  //删除Role
  @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
  @DeleteMapping("remove/{id}")
  public Result removeRole(@PathVariable Integer id){
    boolean isSuccess = roleService.removeById(id);
    if (isSuccess){
      return Result.ok();
    }else {
      return Result.fail();
    }
  }

  //批量删除接口
  //多个id值[1,2,3,4]
  //json数据格式 --java的list集合
  @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
  @DeleteMapping("batchRemove")
  public Result batchRemove(@RequestBody List<Integer> ids){
    boolean isSuccess = roleService.removeByIds(ids);
    if (isSuccess){
      return Result.ok();
    }else {
      return Result.fail();
    }
  }

  //分页查询Role;page表当前页  limit每页记录数
  @PreAuthorize("hasAuthority('bnt.sysRole.list')")
  @GetMapping("{page}/{limit}")
  public Result findQueryTeacher(@PathVariable Integer page,
                                 @PathVariable Integer limit,
                                 RoleQueryVo roleQueryVo){
    //创建Page对象
    Page<Role> pageParam = new Page<>(page, limit);
    //调用service方法
    IPage<Role> pageModel = roleService.selectPage(pageParam,roleQueryVo);
    //返回
    return Result.ok(pageModel);
  }

  //添加Role对象
  //@RequestBody 不能使用get提交方式
  //传递json个是数据，把json格式数据封装到对象里面
  @PreAuthorize("hasAuthority('bnt.sysRole.add')")
  @PostMapping("save")
  public Result saveRole(@RequestBody Role role){
    boolean isSuccess = roleService.save(role);
    if (isSuccess){
      return Result.ok();
    }else {
      return Result.fail();
    }
  }

  //修改 根据id查询
  @PreAuthorize("hasAuthority('bnt.sysRole.list')")
  @GetMapping("findRoleById/{id}")
  public Result findTeacherById(@PathVariable Integer id){
    Role role = roleService.getById(id);
    return Result.ok(role);
  }

  //修改 更新数据库
  @PreAuthorize("hasAuthority('bnt.sysRole.update')")
  @PostMapping("update")
  public Result updateRole(@RequestBody Role role){
    boolean isSuccess = roleService.updateById(role);
    if (isSuccess){
      return Result.ok();
    }else {
      return Result.fail();
    }
  }

  //获取用户角色数据
  @GetMapping("toAssign/{userId}")
  public Result toAssign(@PathVariable Integer userId){
    Map<String,Object> roleMap = roleService.getRoleByUserId(userId);
    return Result.ok(roleMap);
  }

  //分配角色
  @PostMapping("doAssign")
  public Result doAssign(@RequestBody AssignRoleVo assignRoleVo){
    System.out.println(assignRoleVo);
    roleService.doAssign(assignRoleVo);
    return Result.ok();
  }
}
